package com.fun.feifun.modules.sys.auth.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fun.feifun.base.result.Result;
import com.fun.feifun.base.utils.common.TreeUtil;
import com.fun.feifun.modules.sys.auth.dao.SysMenuMapper;
import com.fun.feifun.modules.sys.auth.model.SysMenu;
import com.fun.feifun.modules.sys.auth.model.SysRole;
import com.fun.feifun.modules.sys.auth.model.dto.MenuTree;
import com.fun.feifun.modules.sys.auth.service.ISysMenuService;
import com.fun.feifun.modules.sys.auth.service.ISysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.sql.Array;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统菜单 服务实现类
 * </p>
 *
 * @author wkf
 * @since 2022-04-21
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Autowired
    private ISysRoleService roleService;

    @Override
    public List<MenuTree> getAllTree() {
        QueryWrapper<SysMenu> order=new QueryWrapper();
        order.lambda().orderByAsc(SysMenu::getSort);
        List<MenuTree> all = list(order).stream().map(it->it.toMenuTree()).collect(Collectors.toList());
        List<MenuTree> menuTrees = TreeUtil.listToTree(all);
        return menuTrees;
    }

    @Override
    public List<SysMenu> getListRoleIds(List<Long> roleIds) {
        List<SysMenu> list = baseMapper.getListByRoleIds(roleIds);
        return list;
    }

    @Override
    public List<MenuTree> getTreeByRoleId(Long roleId) {
        List<MenuTree> list = baseMapper.getListByRoleIds(Arrays.asList(roleId)).stream().map(it->it.toMenuTree()).collect(Collectors.toList());;
        List<MenuTree> menuTrees = TreeUtil.listToTree(list);
        return menuTrees;
    }

    @Override
    public List<MenuTree> getTreeByUserId(Long userId) {
        List<SysRole> roles = roleService.getListByUser(Arrays.asList(userId));
        List<Long> roleIds = roles.stream().map(it -> it.getId()).collect(Collectors.toList());
        List<MenuTree> menuList = baseMapper.getListByRoleIds(roleIds).stream().map(it->it.toMenuTree()).collect(Collectors.toList());;
        List<MenuTree> menuTrees = TreeUtil.listToTree(menuList);
        return menuTrees;
    }

    /**
     * 菜单上移
     * @author wukefei
     * @date 2024/3/12 17:34
     * @param item
     */
    public void moveUp(SysMenu item) {
        //修改当前菜单的上一个菜单排序+1
        UpdateWrapper<SysMenu> updateDown=new UpdateWrapper();
        updateDown.lambda()
                .set(SysMenu::getSort,item.getSort())
                .eq(SysMenu::getParentId,item.getParentId())
                .eq(SysMenu::getSort,item.getSort()-1);
        boolean update = update(updateDown);

        //修改当前菜单  排序-1
        UpdateWrapper<SysMenu> updateUp=new UpdateWrapper();
        updateUp.lambda().set(SysMenu::getSort,item.getSort()-1)
                .eq(SysMenu::getId,item.getId());
        boolean update1 = update(updateUp);
        return ;
    }

    /**
     * 菜单下移
     * @author wukefei
     * @date 2024/3/12 17:34
     * @param item
     */
    public void moveDown(SysMenu item) {
        //修改当前菜单的上一个菜单排序+1
        UpdateWrapper<SysMenu> updateUp=new UpdateWrapper();
        updateUp.lambda()
                .set(SysMenu::getSort,item.getSort())
                .eq(SysMenu::getParentId,item.getParentId())
                .eq(SysMenu::getSort,item.getSort()+1);
        boolean update = update(updateUp);

        //修改当前菜单  排序-1
        UpdateWrapper<SysMenu> updateDown=new UpdateWrapper();
        updateDown.lambda().set(SysMenu::getSort,item.getSort()+1)
                .eq(SysMenu::getId,item.getId());
        boolean update1 = update(updateDown);
        return ;
    }

    @Override
    public boolean saveItem(SysMenu item) {
        if(item.getId()==null){
            //新增的时候默认排序最后面
            int maxSort = baseMapper.getMaxSort(item.getParentId());
            item.setSort(maxSort+1);
        }
        return this.saveOrUpdate(item);
    }

    @Override
    public List<Long> getListByRoleId(Long roleId) {
        List<MenuTree> list = baseMapper.getListByRoleIds(Arrays.asList(roleId)).stream().map(it->it.toMenuTree()).collect(Collectors.toList());
        List<Long> ids = list.stream().map(it->it.getId()).collect(Collectors.toList());
        return ids;
    }
}
